package com.andy.common.exception;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * @ClassName ListValConstraintValidator
 * @Description TODO
 * @Auther Andy
 * @Date 2020/11/12 17:23
 * @Version 1.0
 **/
public class ListValConstraintValidator implements ConstraintValidator<ListValue,Integer> {

    private Set<Integer> set=new HashSet<>();
    //初始化方法
    @Override
    public void initialize(ListValue constraintAnnotation) {
        int [] vals=ListValue.vals;
        for (int val : vals){
            set.add(val);
        }
    }

    /**
     * 自定义效验
     * @param integer
     * @param constraintValidatorContext
     * @return set中有默认的值，返回true,没有返回false
     */
    @Override
    public boolean isValid(Integer integer, ConstraintValidatorContext constraintValidatorContext) {
        return set.contains(integer);
    }
}
